package com.qy.StudentInfo.utils;

/**
 * @program: student-info-master
 * @author: QiYue
 * @create: 20220429 23:06
 **/

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.stereotype.Service;

@Service
public class ExcelUtils {
    static Logger log = Logger.getLogger(CommonUtil.class);
    /**
     * 导出Excel
     * @param sheetName sheet名称
     * @param title 标题
     * @param values 内容
     * @param wb HSSFWorkbook对象
     * @return
     */
    public static HSSFWorkbook getHSSFWorkbook(String sheetName,String []title,String [][]values, HSSFWorkbook wb){

        // 第一步，创建一个HSSFWorkbook，对应一个Excel文件
        if(wb == null){
            wb = new HSSFWorkbook();
        }

        //设置大标题样式
        CellStyle headStyle = wb.createCellStyle();	//创建样式
        headStyle.setAlignment(CellStyle.ALIGN_CENTER);	//居中
        headStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
        Font titleFont = wb.createFont();	//创建字体样式
        titleFont.setFontName("Arial");		//设置字体
        titleFont.setFontHeightInPoints((short) 16);	//字体大小
        titleFont.setBoldweight(Font.BOLDWEIGHT_BOLD);	//粗体
        headStyle.setFont(titleFont);

        //设置标题样式--》title
        HSSFCellStyle cellStyle = wb.createCellStyle();
        cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        cellStyle.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.getIndex());	//背景颜色
        cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

        cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);	//边框
        cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
        cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
        Font headerFont = wb.createFont();
        headerFont.setFontName("Arial");
        headerFont.setFontHeightInPoints((short) 12);
        headerFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
        headerFont.setColor(IndexedColors.WHITE.getIndex());	//字体颜色
        cellStyle.setFont(headerFont);

        //设置内容样式
        HSSFCellStyle contentStyle = wb.createCellStyle();
        contentStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        contentStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        contentStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        contentStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
        contentStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
        Font contentFont = wb.createFont();
        contentFont.setFontName("仿宋_GB2312");
        contentFont.setFontHeightInPoints((short) 10);
        contentFont.setColor(IndexedColors.BLACK.getIndex());	//字体颜色
        contentStyle.setFont(contentFont);

        // 第二步，在workbook中添加一个sheet,对应Excel文件中的sheet
        HSSFSheet sheet = wb.createSheet(sheetName);

        // 第三步，在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制
        HSSFRow row = sheet.createRow(0);


        //声明列对象
        HSSFCell cell = null;

        cell = row.createCell(0);
        cell.setCellValue("学生选课情况");
        cell.setCellStyle(headStyle);

        //创建标题
        row = sheet.createRow(1);
        for(int i=0;i<title.length;i++){
            cell = row.createCell(i);
            cell.setCellValue(title[i]);
            cell.setCellStyle(cellStyle);
        }

        //创建内容
        for(int i=0;i<values.length;i++){
            row = sheet.createRow(i + 2);
            for(int j=0;j<values[i].length;j++){
                //将内容按顺序赋给对应的列对象
                HSSFCell cCell = row.createCell(j);
                cCell.setCellValue(values[i][j]);
                cCell.setCellStyle(contentStyle);
            }
        }

        CellRangeAddress region = new CellRangeAddress(0,0,0,title.length-1); //大标题合并居中
        sheet.addMergedRegion(region);
        //设置行宽
        for (int i = 0; i < title.length; i++) {
            sheet.setColumnWidth(i,4000);
        }

        return wb;
    }
}
